package com.edu.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.edu.dao.BaseDao;
import com.edu.dao.OrdersDao;
import com.edu.entity.Orders;

public class OrdersDaoImpl implements OrdersDao {
	private BaseDao bd1 = new BaseDao();

	@Override
	public List<Orders> selectAllOrders(int target_user_id) {
		ArrayList<Orders> uclist = new ArrayList();
		Connection conn = bd1.getConn();
		String sql = "SELECT * FROM orders WHERE 1=1 ";
		if(target_user_id!=0) {
			sql +=" AND target_user_id = ?";
		}
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			if(target_user_id!=0) {
				ps.setInt(1, target_user_id);
			}
			System.out.println(ps);
			rs = ps.executeQuery();
			while(rs.next()) {
				Orders o = new Orders();
				o.setOrdersId(rs.getInt("orders_id"));
				o.setUserId(rs.getInt("user_id"));
				o.setTargetUserId(rs.getInt("target_user_id"));
				o.setDatatime(rs.getDate("datatime"));
				o.setCommodityId(rs.getInt("commodityId"));
				o.setOrderNo(rs.getString("orderNo"));
				o.setMoney(rs.getDouble("money"));
				o.setPayMethod(rs.getInt("payMethod"));
				o.setState(rs.getInt("state"));
				o.setShipmentDate(rs.getDate("shipmentDate"));
				uclist.add(o);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			bd1.closeAll(conn, ps, rs);
		}
		return uclist;
	}

	@Override
	public int selectAllOrdersNums(int target_user_id) {
		Connection conn = bd1.getConn();
		int rows = 0;
		String sql = "SELECT count(1) as totalRow FROM orders WHERE 1=1 ";
		if(target_user_id!=0) {
			sql +=" AND target_user_id = ?";
		}
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			if(target_user_id!=0) {
				ps.setInt(1, target_user_id);
			}
			System.out.println(ps);
			rs = ps.executeQuery();
			if(rs.next()) {
				rows = rs.getInt("totalRow");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			bd1.closeAll(conn, ps, rs);
		}
		return rows;
	}

	@Override
	public int sqlAddOrder(Orders o) {
		// TODO Auto-generated method stub
		System.out.println("aaaa");
		String sql = "INSERT INTO `orders` (`user_id`, `target_user_id`, `addressid`, `datatime`, " +
	             "`commodityId`, `orderNo`, `money`, `payMethod`, `state`) " +
	             "VALUES (?, ?, ?, NOW(), ?, ?, ?, ?, ?)";
		Object[] obj = {o.getUserId(),o.getTargetUserId(),o.getAddressid(),o.getCommodityId(),o.getOrderNo(),o.getMoney(),o.getPayMethod(),o.getState()}; 
		return bd1.executeUpdate(sql, obj);
	}

	@Override
	public int deletOrder(int orderId) {
		Connection conn = bd1.getConn();
		int rows = -1;
		String sql = "DELETE FROM orders WHERE orders_id = ?"; 
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, orderId);
			rows = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			bd1.closeAll(conn, ps, rs);
		}
		return rows;
	}

	@Override
	public int updateOrder(int orderId,int state) {
		Connection conn = bd1.getConn();
		int rows = -1;
		String sql = "UPDATE orders SET state = ? WHERE orders_id = ?"; 
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, state);
			ps.setInt(2, orderId);
			System.out.println("语句为："+ps);
			rows = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			bd1.closeAll(conn, ps, rs);
		}
		return rows;
	}

}
